﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;
using System.Net.Sockets;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        string LocalName,PageWithIP,IP,InvMessage;
        public Form1()
        {
            InitializeComponent();
            label11.Text = "" + a;
        }

        private void StartButton_Click(object sender, EventArgs e)
        {
            LocalName = Dns.GetHostName();
            tbName.Text = LocalName;
            GlobalIP.Text = GetPublicIP();
            label4.Text = GetLocalIP();
        }

        private string GetPublicIP()
        {
            WebRequest wreq = WebRequest.Create("http://checkip.dyndns.org/");
            WebResponse wres = wreq.GetResponse();
            StreamReader sr = new StreamReader(wres.GetResponseStream());
            PageWithIP = sr.ReadToEnd();
            int nach = PageWithIP.IndexOf("Address: ") + 9;
            int kon = PageWithIP.LastIndexOf("</body>");
            IP = PageWithIP.Substring(nach, kon - nach);
            return IP;
        }

        private string GetLocalIP()
        {
            string LocalIP2 = "";
            IPHostEntry LocalIP1 = Dns.GetHostEntry(LocalName);
            for (int i = 0; i < LocalIP1.AddressList.Length; i++)
            {
                LocalIP2 = LocalIP2 + LocalIP1.AddressList[i] + "\n";
            }
            return LocalIP2;
        }
        //*********************************************************************************
        //Конец вкладки "Данные о вас"
        //*********************************************************************************
        TcpListener listener;
        int a = 0;
        Boolean check = true;
        string [] sendips = new string [20];
        string [] sendports = new string[20];

        private void StartListening()
        {
            listener = new TcpListener(IPAddress.Any, Convert.ToInt16(TBMyPort.Text));
            listener.Start();
            bw.RunWorkerAsync();
        }

        private void bw_DoWork(object sender, DoWorkEventArgs e)
        {
            TcpClient client = listener.AcceptTcpClient();
            StreamReader reader = new StreamReader(client.GetStream());

            InvMessage = client.Client.RemoteEndPoint.ToString() + " > " + reader.ReadToEnd();
        }

        private void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            LBChat.Items.Add(InvMessage);
            bw.RunWorkerAsync();
        }

        private void ListenButton_Click(object sender, EventArgs e)
        {
            StartListening();
            ListenButton.Enabled = false;
        }

        private void Send()
        {
            try
            {
                for (int i = 0; i > 20; i++)
                {
                    if (sendips[i] == "") 
                    {
                        break;
                    }
                    TcpClient client = new TcpClient();
                    client.Connect(IPAddress.Parse(sendips[i]), Convert.ToInt16(sendports[i]));
                    StreamWriter writer = new StreamWriter(client.GetStream());
                    writer.Write(Nick.Text + ":" + TBChatSend.Text);
                }
            }
            catch
            {
            }
        }

        private void SendButton_Click(object sender, EventArgs e)
        {
            Send();
        }

        private void Addbutton_Click(object sender, EventArgs e)
        {
            if (check == true)
            {
                for (int i = 0; i < 20; i++)
                {
                    sendips[i] = "";
                    sendports[i] = "";
                }
                check = false;
            }
            sendips[a] = TBSenderIP.Text;
            sendports[a] = TBSenderPort.Text;
            a++;
            label11.Text = "" + a;
        }

        private void TBChatSend_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == Convert.ToChar(Keys.Enter))
            {
                Send();
            }
        }
    }
}
